﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Globalization;
using System.Threading;

namespace SqlMonitoring.Agent
{
    public partial class AgentService : ServiceBase
    {
        static bool isCommandline = false;

        public AgentService()
        {
            ServiceName = "SqlMonitoring.Agent";
        }

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                foreach (string option in args)
                {
                    switch (option.ToLower(CultureInfo.InvariantCulture))
                    {
                        case "d":
                        case "-d":
                        case "debug":
                        case "-debug":
                            isCommandline = true;
                            break;
                    }
                }
            }

            if (isCommandline)
            {
                AgentService agentService = new AgentService();
                agentService.StartService();
                Thread.Sleep(Timeout.Infinite);
            }
            else
            {
                ServiceBase.Run(new AgentService());
            }
        }

        protected override void OnStart(string[] args)
        {
            MainService main = new MainService();
            main.Init();
            Thread.Sleep(1000); //Give time to agents to start.
            TimerCallback timerDelegate =
                new TimerCallback( main.MinuteChecks);
            Timer timer = new System.Threading.Timer(timerDelegate, isCommandline, 1000, 60000);

        }

        protected override void OnStop()
        {
        }

        void StartService()
        {
            OnStart(new string[] { });
        }

        void StopService()
        {
            OnStop();
        }
    }
}
